<?php
namespace app\admin\controller;
use think\Controller;
use think\Db;
class Activity extends Common
{
    public function index(){
        $activity=Db::name("Activities")->select();
        $this->assign("activity",$activity);

        return $this->fetch();
    }






    public function form(){
        return $this->fetch();
    }




    public function add(){

        //处理上传
        $file = request()->file('ac_pic');
        $ac_pic="";
//移动到框架应用根目录/public/uploads/目录下
        if($file){
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'.DS.'images');

            if($info){
                $ac_pic=$info->getFilename();
            }else{
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }


       $data=[
            'ac_name'=>input('ac_name'),
           'ac_address'=>input('ac_address'),
            'ac_time'=>input('ac_time'),
            'ac_num'=>input('ac_num'),
            'ac_price'=>input('ac_price'),
           'ac_intro'=>input('ac_intro'),
           'ac_tel'=>input('ac_tel'),
              'ac_pic'=>empty($ac_pic)?"1.jpg":$ac_pic,
       ];
        if (Db::name('Activities')->insert($data)){
           $this->success('添加成功','Activity/index');
       }else $this->error('添加失败');
        return $this->fetch();
    }

    public function mod($id){
        $activity=Db::name("Activities")->find($id);
        $this->assign("activity",$activity);
        return $this->fetch();
    }
    public function edit(){
        //图片是否修改
        //获取其他的表单数据

        //在数据库里修改
        $file = request()->file('ac_pic');
        //$ac_pic = "";
        //dump($file);exit;
        $ccc=input('ac_pic');

        if(!$ccc){
            $ccc1=input('ac_pic1');
        }


        // 移动到框架应用根目录/public/uploads/ 目录下
        if($file){
            //dump($file);
            $info = $file->rule('uniqid')->validate(['size'=>3000000,'ext'=>'jpg,png,gif,jpeg'])->move(ROOT_PATH . 'public' . DS . 'static'.DS.'admin'. DS . 'images');
            // ;exit;
            if($info){
                $ccc1=$info->getFileName();
                //dump($pic);
            }else{
                // 上传失败获取错误信息  //
                echo $file->getError();
            }
        }




        $data=[
            'ac_id'=>input('ac_id'),
            'ac_name'=>input('ac_name'),
            'ac_address'=>input('ac_address'),
            'ac_time'=>input('ac_time'),
            'ac_num'=>input('ac_num'),
            'ac_price'=>input('ac_price'),
            'ac_intro'=>input('ac_intro'),
            'ac_tel'=>input('ac_tel'),
              'ac_pic'=>$ccc1,
        ];
        if (Db::name('Activities')->update($data)){
            $this->success('修改成功','Activity/index');
        }else $this->error('修改失败');
        return $this->fetch();
    }



    public function delete($id){
        if (Db::name("Activities")->delete($id)){
            $this->success('删除成功');
        }else $this->error('删除失败');
    }

    public function search(){
        $key=input('key');
       // dump($key);exit;
        if(!$key){
            $this->success('无搜索内容','Activity/index');
    }else{
            $activity=Db::table('Activities')->where('ac_name','like','%'.$key.'%')->select();
            $this->assign("activity",$activity);
            // dump($activity);exit;
            return $this->fetch('index');

        }

        // dump($sql);
    }
     public function jiekou(){

        $rs=Db::name('Activities')->select();
        return json($rs);
}





}


